﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// MLine.xaml 的交互逻辑
    /// </summary>
    public partial class MLine : UserControl
    {
        
        public PointCollection DataPoints
        {
            get { return (PointCollection)GetValue(DataPointsProperty); }
            set { SetValue(DataPointsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DataPoints.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataPointsProperty =
            DependencyProperty.Register("DataPoints", typeof(PointCollection), typeof(MLine), new PropertyMetadata(new PointCollection() { 
                new Point(0,50),
                new Point(30,0),
                new Point(200,50),
                 new Point(200,200)
            }));


        public MLine()
        {
            InitializeComponent();
            this.Loaded += MarksLine_Loaded;
        }
        void MarksLine_Loaded(object sender, RoutedEventArgs e)
        {
            AddThumb();

        }
        private Thumb thumb1, thumb2, thumb3, thumb4;

        private void AddThumb()
        {
            Ellipse e1 = new Ellipse() { Width=10,Height=10,Fill=Brushes.Green};
            Ellipse e2 = new Ellipse() { Width = 32, Height = 32, Fill = Brushes.Green, Opacity = 0.5 };

            GdMain.Children.Add(e1);
            GdMain.Children.Add(e2);

            thumb1 = new Thumb();
            thumb1.Width = 10;
            thumb1.Height = 10;

            Canvas.SetLeft(thumb1, DataPoints[0].X - thumb1.Width / 2);
            Canvas.SetTop(thumb1, DataPoints[0].Y - this.thumb1.Height / 2);


            thumb1.Template = GetResizeThumbControlTemplate();
            thumb1.HorizontalAlignment = HorizontalAlignment.Left;
            thumb1.VerticalAlignment = VerticalAlignment.Top;
            thumb1.DragDelta += thumb1_DragDelta;
            GdMain.Children.Add(thumb1);


            e1.SetBinding(Canvas.LeftProperty, new Binding("(Canvas.Left)") { Source = thumb1 });
            e1.SetBinding(Canvas.TopProperty, new Binding("(Canvas.Top)") { Source = thumb1 });
            e2.SetBinding(Canvas.LeftProperty, new Binding("(Canvas.Left)") { Source = thumb1 });
            e2.SetBinding(Canvas.TopProperty, new Binding("(Canvas.Top)") { Source = thumb1 });


            thumb2 = new Thumb();
            thumb2.Width = 10;
            thumb2.Height = 10;
            //thumb2.Margin = new Thickness(this.SecondPoint.X - thumb2.Width / 2, SecondPoint.Y - this.thumb2.Height / 2, 0, 0);
            Canvas.SetLeft(thumb2, DataPoints[1].X - thumb2.Width / 2);
            Canvas.SetTop(thumb2, DataPoints[1].Y - this.thumb2.Height / 2);


            thumb2.Template = GetResizeThumbControlTemplate();
            thumb2.HorizontalAlignment = HorizontalAlignment.Left;
            thumb2.VerticalAlignment = VerticalAlignment.Top;
            thumb2.DragDelta += thumb2_DragDelta;
            GdMain.Children.Add(thumb2);
            
            
            thumb3 = new Thumb();
            thumb3.Width = 10;
            thumb3.Height = 10;

            
            Canvas.SetLeft(thumb3, DataPoints[2].X - thumb3.Width / 2);
            Canvas.SetTop(thumb3, DataPoints[2].Y - this.thumb3.Height / 2);
            thumb3.Template = GetResizeThumbControlTemplate();
            thumb3.HorizontalAlignment = HorizontalAlignment.Left;
            thumb3.VerticalAlignment = VerticalAlignment.Top;
            thumb3.DragDelta += thumb3_DragDelta;
            GdMain.Children.Add(thumb3);

            thumb4 = new Thumb();
            thumb4.Width = 10;
            thumb4.Height = 10;


            Canvas.SetLeft(thumb4, DataPoints[3].X - thumb4.Width / 2);
            Canvas.SetTop(thumb4, DataPoints[3].Y - this.thumb4.Height / 2);
            thumb4.Template = GetResizeThumbControlTemplate();
            thumb4.HorizontalAlignment = HorizontalAlignment.Left;
            thumb4.VerticalAlignment = VerticalAlignment.Top;
            thumb4.DragDelta += thumb4_DragDelta;
            GdMain.Children.Add(thumb4);
            

        }

        private PointCollection tmpPdata = new PointCollection();

        void thumb4_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb mth = (Thumb)sender;
            double y = Canvas.GetTop(mth) + e.VerticalChange;
            double x = Canvas.GetLeft(mth) + e.HorizontalChange;
            Canvas.SetTop(mth, Canvas.GetTop(mth) + e.VerticalChange);
            Canvas.SetLeft(mth, Canvas.GetLeft(mth) + e.HorizontalChange);

            tmpPdata = this.DataPoints;
            this.DataPoints = new PointCollection() 
            { 
                tmpPdata[0],
                tmpPdata[1],
                tmpPdata[2],
                new Point(x + this.thumb4.Width / 2, y + this.thumb4.Height / 2),
            };
        }

        void thumb3_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb mth = (Thumb)sender;
            double y = Canvas.GetTop(mth) + e.VerticalChange;
            double x = Canvas.GetLeft(mth) + e.HorizontalChange;
            Canvas.SetTop(mth, Canvas.GetTop(mth) + e.VerticalChange);
            Canvas.SetLeft(mth, Canvas.GetLeft(mth) + e.HorizontalChange);
            //this.DataPoints[2] = new Point(x + this.thumb3.Width / 2, y + this.thumb3.Height / 2);
            tmpPdata = this.DataPoints;
            this.DataPoints = new PointCollection() 
            { 
                tmpPdata[0],
                tmpPdata[1],
                new Point(x + this.thumb3.Width / 2, y + this.thumb3.Height / 2),
                tmpPdata[3]
            };
        }

        void thumb2_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb mth = (Thumb)sender;
            double y = Canvas.GetTop(mth) + e.VerticalChange;
            double x = Canvas.GetLeft(mth) + e.HorizontalChange;
            Canvas.SetTop(mth, Canvas.GetTop(mth) + e.VerticalChange);
            Canvas.SetLeft(mth, Canvas.GetLeft(mth) + e.HorizontalChange);
            //this.DataPoints[1] = new Point(x + this.thumb2.Width / 2, y + this.thumb2.Height / 2);
            tmpPdata = this.DataPoints;
            this.DataPoints = new PointCollection() 
            { 
                tmpPdata[0],
                new Point(x + this.thumb2.Width / 2, y + this.thumb2.Height / 2),
                tmpPdata[2],
                tmpPdata[3]
            };
        }

        void thumb1_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb mth = (Thumb)sender;
            double y = Canvas.GetTop(mth) + e.VerticalChange;
            double x = Canvas.GetLeft(mth) + e.HorizontalChange;
            Canvas.SetTop(mth, Canvas.GetTop(mth) + e.VerticalChange);
            Canvas.SetLeft(mth, Canvas.GetLeft(mth) + e.HorizontalChange);
            //this.DataPoints[0] = new Point(x + this.thumb1.Width / 2, y + this.thumb1.Height / 2);
            tmpPdata = this.DataPoints;
            this.DataPoints = new PointCollection() 
            {   
                tmpPdata[0],
                new Point(x + this.thumb1.Width / 2, y + this.thumb1.Height / 2),
                tmpPdata[1],
                tmpPdata[2]
            };
        }
        private ControlTemplate GetResizeThumbControlTemplate()
        {
            var factory = new FrameworkElementFactory(typeof(Rectangle));
            factory.SetValue(Shape.FillProperty, Brushes.Black);
            factory.SetValue(Shape.StrokeProperty, Brushes.Black);
            var controlTemplate = new ControlTemplate { TargetType = typeof(Thumb), VisualTree = factory };

            return controlTemplate;
        }
        bool flag = true;
        private void PlineMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(flag)
            {
                thumb1.Visibility = Visibility.Collapsed;
                thumb2.Visibility = Visibility.Collapsed;
                thumb3.Visibility = Visibility.Collapsed;
                flag = !flag;
            }
            else
            {
                thumb1.Visibility = Visibility.Visible;
                thumb2.Visibility = Visibility.Visible;
                thumb3.Visibility = Visibility.Visible;
                flag = !flag;
            }
        }
    }
}
